package com.demo.web;

import com.demo.pojo.User;
import com.demo.service.TestService;
import com.demo.task.ThreadTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

@RestController
@RequestMapping("/test")
public class Test01Controller {
    @Autowired
    private TestService service;

    @RequestMapping("/01")
    public Map<String, Integer> test01(User user) {
        System.out.println(user);
        //创建线程任务类
        ThreadTask task = new ThreadTask(service, user);
        //开启线程
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                3,
                5,
                100,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(6),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        for (int i = 0; i < 10; i++) {
            poolExecutor.submit(task);
        }

        //关闭线程池
        //poolExecutor.shutdown();
        //封装返回值
        Map<String, Integer> map = new HashMap<>();
        map.put("操作状态", 200);
        return map;
    }

    @RequestMapping("/02")
    public Map<String, Integer> test02(User user) {
        service.updateMoney(user.getId(), user.getMoney());
        Map<String, Integer> map = new HashMap<>();
        map.put("操作状态", 200);
        return map;
    }
}
